package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

public class Biblioteca {

    private List<Book> listBooks = new ArrayList<Book>();
    private List<Movie> listMovies = new ArrayList<Movie>();

    public Biblioteca() {
        addBooksToTheLibrary();
        addMoviesToTheLibrary();
    }

    public void addBooksToTheLibrary() {
        Book bookGalaxy = new Book();
        bookGalaxy.setName("The Galaxy");
        bookGalaxy.setAuthor("Abigail S.");
        bookGalaxy.setYearPublished(2010);

        Book bookCode = new Book();
        bookCode.setName("Code School");
        bookCode.setAuthor("Bart C.");
        bookCode.setYearPublished(2014);

        Book bookCook = new Book();
        bookCook.setName("The chef");
        bookCook.setAuthor("Camilla D.");
        bookCook.setYearPublished(2013);

        listBooks.add(bookGalaxy);
        listBooks.add(bookCode);
        listBooks.add(bookGalaxy);
    }

    public void addMoviesToTheLibrary() {
        Movie movieHorror = new Movie();
        movieHorror.setName("The Exorcist");
        movieHorror.setDirector("Johan");
        movieHorror.setMovieRating("1");
        movieHorror.setYear(2012);

        Movie movieComedy = new Movie();
        movieComedy.setName("American Pie");
        movieComedy.setDirector("Sean");
        movieComedy.setMovieRating("3");
        movieComedy.setYear(2013);

        Movie movieTragedy = new Movie();
        movieTragedy.setName("Forest Gump");
        movieTragedy.setDirector("Steven");
        movieTragedy.setMovieRating("9");
        movieTragedy.setYear(2011);

        listMovies.add(movieHorror);
        listMovies.add(movieHorror);
        listMovies.add(movieTragedy);
    }

    public String showTheBooksOnTheLibrary() {
        String listBooks = "\n---- List of books ----";
        for (Book book : this.listBooks) {
            listBooks += "\nBook: " + book.getName();
            listBooks += "\nAuthor: " + book.getAuthor();
            listBooks += "\nPublished in " + book.getYearPublished();
            listBooks += "\n_________________";
        }
        return listBooks;
    }

    public String checkOutBook(int bookNumber) {
        if (isValidBook(bookNumber)) {
            listBooks.remove(bookNumber);
            return "Thank you! Enjoy the book.";
        } else
            return "That book is not available.";
    }

    public String returnBook(Book bookReturning) {
        int quantityBooksOnLibraryBeforeAdd = listBooks.size();
        listBooks.add(bookReturning);

        if (quantityBooksOnLibraryBeforeAdd == listBooks.size())
            return "That is not a valid book to return.";
        else
            return "Thank you for returning the book.";
    }

    public boolean isValidBook(int book) {
        if (book >= 0 && book < listBooks.size())
            return true;
        else
            return false;
    }

    public String showTheMoviesOnTheLibrary() {
        String listMovies = "\n---- List of Movies ----";
        for (Movie movie : this.listMovies) {
            listMovies += "\nMovie: " + movie.getName();
            listMovies += "\nDirector: " + movie.getDirector();
            listMovies += "\nRate: " + movie.getMovieRating();
            listMovies += "\nDisplayed in " + movie.getYear();

            listMovies += "\n_________________";
        }
        return listMovies;
    }

    public List<Book> getListBooks() {
        return this.listBooks;
    }

    public List<Movie> getListMovies() {
        return this.listMovies;
    }

    public String checkOutMovie(int movieNumber, User user) {
        if (isValidMovie(movieNumber)) {
            listMovies.remove(movieNumber);
            return "Thank you " + user.getRegistration() + " ! Enjoy the movie.";
        } else
            return "That movie is not available.";
    }

    private boolean isValidMovie(int movieNumber) {
        if (movieNumber >= 0 && movieNumber < listMovies.size())
            return true;
        else
            return false;
    }
}
